package server.players;


import java.util.ArrayList;
import java.util.List;

import kryonetpackets.RGB;



public class Player {
	private int id;
	private String name;
	private int connectionId;
	private List<PlayerGroup> currentGroups;
	private RGB color;
	
	public Player(int id, String name, RGB color, int connectionId)
	{
		this.id = id;
		this.name = name;
		this.color = color;
		this.connectionId = connectionId;
		
		this.currentGroups = new ArrayList<PlayerGroup>();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getConnectionId() {
		return connectionId;
	}

	public void setConnectionId(int connectionId) {
		this.connectionId = connectionId;
	}
	public void removeFromAllPlayerGroups()
	{
		for (int i = 0; i < this.currentGroups.size(); i++)
		{
			try {
				this.removeFromPlayerGroup(this.currentGroups.get(i));
			} catch (PlayerGroupingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Makes this Player store what PlayerGroups it is currently a part of.
	 * 
	 * This function should only be called by the PlayerGroup object.
	 * 
	 * @param pg The PlayerGroup
	 * @throws PlayerGroupingException If param pg does not know about this Player.
	 */
	public void addToPlayerGroup(PlayerGroup pg) throws PlayerGroupingException {
		if( pg.hasPlayer(this) ) {
			if( !this.currentGroups.contains(pg) ) {
				currentGroups.add(pg);
			}
		} else {
			throw new PlayerGroupingException("Player trying define itself "
					+ "as a member of a PlayerGroup is isn't a part of");
		}
	}
	
	/**
	 * Removes the PlayerGroup from this Player's storage.
	 * 
	 * This function should only be called by the PlayerGroup object.
	 * 
	 * @param pg The PlayerGroup
	 * @throws PlayerGroupingException If this Player is not part of param pg
	 */
	public void removeFromPlayerGroup(PlayerGroup pg) throws PlayerGroupingException {
		if( pg.hasPlayer(this) ) {
			if( this.currentGroups.contains(pg) ) {
				currentGroups.remove(pg);
			}
		} else {
			throw new PlayerGroupingException("Player trying to remove itself "
					+ "from a PlayerGroup is isn't a part of");
		}
	}

	public RGB getColor() {
		return color;
	}

	public void setColor(RGB color) {
		this.color = color;
	}
	

}
